package com.neusoft.supermarket.UI;

import com.neusoft.supermarket.common.File_Uitl;
import com.neusoft.supermarket.common.Init_DataBase;
import com.neusoft.supermarket.entity.UserInfo_Entity;
import com.neusoft.supermarket.service.UserInfo_Service;

import java.util.Scanner;

public class MainUI {

	 Scanner sc = new Scanner(System.in);
	 public void stup(){
	 	String str = File_Uitl.readFile("stup.txt");
	 	if(null!=str&&str.equals("Y")){
	 		open_system();
	 	}else{
	 		init_system();
	 	}
	 }

	 public void init_system(){

		 System.out.println("是否安装美美超市收银系统");
		 System.out.println("是(Y)\t否(N)");
		 String str = sc.next();
		 if(str.equals("Y")||str.equals("y")){
			 Init_DataBase.exec_shellsql();
			 File_Uitl.writeFile("stup.txt");
		 }else{
			 System.out.println("程序没有安装成功,退回安装!!");
			 return;
		 }

		 System.out.println("程序安装完毕!!!");
		 open_system();
	 }

	 public void open_system(){
		 System.out.println("是否立即打开程序?是(Y)\t否(N)");
		 String str = sc.next();
		 if(str.equals("Y")||str.equals("y")){
			 mainMenu();
		 }else
		 {
			 return;
		 }
	 }
	/**
	 * 主菜单
	 */
	public  void mainMenu() {
		System.out.println("欢迎登陆xxxx超市收银系统");
		System.out.println("1.管理员");
		System.out.println("2.收银");
		System.out.println("3.退出");
		System.out.println("请输入你的选择：(输入数字1-3)");

		int a = sc.nextInt();
		switch (a) {
			case 1:
				Login();
				break;
			case 2:
				Login();
				break;
			case 3:
				System.exit(0);
				break;
		}
	}

	public  void Login(){

		System.out.print("请输入用户：\t");
		String user = sc.next();
		System.out.print("请输入密码：\t");
		String password = sc.next();
		UserInfo_Service service = new UserInfo_Service();
		UserInfo_Entity userInfo = service.doLogin(user,password);

		if(null != userInfo)
		{
			if(userInfo.getIsadmin().equals("Y")||userInfo.getIsadmin().equals("y"))
			{
				AdminUI admin = new AdminUI();
				admin.adminMenu();
			}else{
				if (userInfo.getIsenable().equals("Y")||userInfo.getIsenable().equals("y"))
				{
					EmployeeUI employeeUI = new EmployeeUI();
					employeeUI.userUI();
				}else{
					System.out.println("账户被禁用,请使用其他账户登录");
					Login();
				}
			}
		}else{
			System.out.println("你输入的用户名或者密码错误!!!");
			System.out.println("请重新输入!!!!");
			Login();
		}
	}
}
